package com.itqf.controller;

import com.itqf.entity.User;
import com.itqf.enums.Rexum;
import com.itqf.service.UserService;
import com.itqf.utils.R;
import com.itqf.vo.ResultVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 作者:高少东
 * 项目名:  product_gsd
 * 时间: 2019/12/18 20:21
 * 描述:
 */


@Controller
public class UserController {

    @Autowired
    UserService userService;



    /**
     * 查询账号是否存在,不存在方可注册
     */
    @PostMapping("queryAccount")
    @ResponseBody
     public ResultVo queryAccount(String uAccount){

        String account = userService.queryAccount(uAccount);

        if (account==null){

           return R.ok();
        }else {
            return R.error(Rexum.Account_EXIST);
        }

     }


    /**
     * 注册账号
     */

    @PostMapping("registAccount")
    @ResponseBody
     public ResultVo registAccount(User user){

        if (StringUtils.isEmpty(user.getUAccount())|| StringUtils.isEmpty(user.getUPassword())){

            return R.error(Rexum.PARAMPER_ERROR);
        }

        int rows =userService.registAccount(user);

         if (rows>0){

             return  R.ok();
         }else{

             return  R.error(Rexum.NOT_KNOW_ERROR);
         }


     }


    /**
     * 登录账号  shiro 认证
     */

      @RequestMapping("loginAccount")
      @ResponseBody
      public ResultVo loginAccount(User user, HttpSession session){

          if (StringUtils.isEmpty(user.getUAccount()) || StringUtils.isEmpty(user.getUPassword())){
              return R.error(Rexum.PARAMPER_ERROR);
          }

          UsernamePasswordToken passwordToken = new UsernamePasswordToken(user.getUAccount(),user.getUPassword());

           Subject subject = SecurityUtils.getSubject();



           try {
               subject.login(passwordToken);
               session.setAttribute("pwd",user.getUPassword());
               User  user1 = (User) subject.getPrincipal();

               session.setAttribute("user",user1);

           }catch (AccountException e){

               return R.error(Rexum.ACCOUNT_ERROR);
           }catch (IncorrectCredentialsException e){
               return R.error(Rexum.PASSWORD_ERROR);
           }catch (Exception e){

               return  R.error(Rexum.NOT_KNOW_ERROR);
           }

          return  R.ok();

      }


      /*
          修改密码  为空不执行  修改后密码与原密码相同不执行
       */


       @PostMapping("alterPassword")
       @ResponseBody
 public ResultVo alterPassword(Integer uId,String oldPassword,String newPassword,String newsPassword) {

           if (StringUtils.isEmpty(oldPassword) || StringUtils.isEmpty(newPassword)
                   || StringUtils.isEmpty(newsPassword)) {

               return R.error(Rexum.PARAMPER_ERROR);
           }

           if (oldPassword.equalsIgnoreCase(newPassword)){

               return R.error(7,"未设置新的密码");
           }

           if (!newsPassword.equalsIgnoreCase(newPassword)){

               return R.error(8,"请确认修改后的密码是否一致");
           }

           int rows = userService.alterPassword(uId,newPassword);


            if (rows>0){

                return R.ok();
            }else{

                return R.error(Rexum.NOT_KNOW_ERROR);
            }


       }


    /**
     *  登录验证 账号是超管 还是普通管理员
     */


    @PostMapping("verifyUser")
    @ResponseBody
     public ResultVo verifyUser(String uAccount){

        if (StringUtils.isEmpty(uAccount)){

            return R.error(Rexum.PARAMPER_ERROR);
        }

       String uaccount =userService.verifyUser(uAccount);

        if (StringUtils.isEmpty(uaccount)){
            return R.error(Rexum.ACCOUNT_ERROR);
        }

       if (uAccount.equalsIgnoreCase("root")){

           return R.ok();
       }else {

           return  R.error(Rexum.NOT_KNOW_ERROR);
       }


     }
}
